#message sender module
#https://docs.python.org/3/library/xml.etree.elementtree.html#module-xml.etree.ElementTree
import pika
import sys
import  time
import threading
import queue


class PostMan(threading.Thread):

    def __init__(self,queueName,exchangeName=''):
        threading.Thread.__init__(self)
        self.queue = queue.Queue()
        self.connection = pika.BlockingConnection(pika.ConnectionParameters(
            host='localhost'))
        self.channel = self.connection.channel()
        self.channel.queue_declare(queue=queueName, durable=True)  # declare a durable channel
        self.quene=queueName
        self.exchange=exchangeName

    def postMail(self,content):
        self.channel.basic_publish(exchange=self.exchange,
                          routing_key=self.quene,
                          body=content,
                          properties=pika.BasicProperties(
                              delivery_mode=2,  # make message persistent
                          ))

    def run(self):
        while True:
            item = self.queue.get()
            if item is None:
                break
            self.postMail(item)
            self.queue.task_done()

    def deliver(self,content):
        self.queue.put(content)
